<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(date, price, monthNumber) {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        color: ['#4fd7fd', '#b2f567'],
        legend: {},
        toolbox: {
          iconStyle: {
            color: 'white',
            borderColor: 'grey'

          },
          show: true,
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        tooltip: {
          trigger: 'axis',
          formatter: function(params) {
            return params[0].name + '<br>' +
                    params[0].marker + '当月' + params[0].seriesName + '：' + params[0].data + '元' + '<br>' +
                    params[1].marker + '当月' + params[1].seriesName + '：' + params[1].data + '次'
          },
          animation: true,
          animationDuration: 20000,
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: 'red'
            }
          }
        },

        grid: {
          show: false,
          backgroundColor: 'white',
          borderColor: 'black',
          borderWidth: 1
        },
        xAxis: [{
          type: 'category',
          data: date,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
            textStyle: {
              fontSize: '12',
              color: 'red'
            }
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: 'black',
              width: 1,
              type: 'solid'
            }
          }

        }],
        yAxis: [{
          splitLine: { show: false },
          name: '元',
          type: 'value',
          axisLabel: {
            textStyle: {
              fontSize: '12',
              color: 'red'
            }
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: 'black',
              width: 1,
              type: 'solid'
            }
          }
        }, {
          name: '次',
          type: 'value',
          axisLabel: {
            textStyle: {
              fontSize: '12',
              color: 'red'
            }
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: 'black',
              width: 1,
              type: 'solid'
            }
          }
        }
        ],
        series: [
          {
            name: '总消费',
            type: 'bar',
            data: price,
            barWidth: 15,
            markPoint: {
              data: [{ type: 'max' }]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          },
          {
            name: '总次数',
            type: 'line',
            yAxisIndex: 1,
            data: monthNumber,
            barWidth: 15
          }
        ]
      })
      var that = this
      this.chart.on('click', function(params) {
        that.$router.push({
          path: '/daily/spend',
          query: { date: params.name }
        })
      })
    }
  }
}
</script>
